package com.hyf.statis.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hyf.commonutils.fruit;
import com.hyf.statis.client.UcenterClient;
import com.hyf.statis.entity.StatisticsDaily;
import com.hyf.statis.mapper.StatisticsDailyMapper;
import com.hyf.statis.service.StatisticsDailyService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 网站统计日数据 服务实现类
 * </p>
 *
 * @author hyf
 * @since 2022-05-23
 */
@Service
public class StatisticsDailyServiceImpl extends ServiceImpl<StatisticsDailyMapper, StatisticsDaily> implements StatisticsDailyService {
	
	
	@Autowired
	private UcenterClient ucenterClient;// 远程调用用户中心的接口
	
	@Override
	public fruit registerCount(String day) {
		
		// 删除已经存在的数据，先删除原来的数据，再插入新的数据
		QueryWrapper<StatisticsDaily> dayQueryWrapper = new QueryWrapper<>();
		dayQueryWrapper.eq("date_calculated", day);
		baseMapper.delete(dayQueryWrapper);
		
		// 远程调用得到某一天注册人数
		 fruit registerCount = ucenterClient.registerCount(day);
		Integer  registerint=(Integer) registerCount.getData().get("countRegister");
		
		// 把获取到的数据添加到统计分析表里面
		StatisticsDaily statisticsDaily = new StatisticsDaily();
		statisticsDaily.setRegisterNum(registerint);// 注册人数
		statisticsDaily.setDateCalculated(day);// 统计日期
		
		
		// 添加一个虚假的流量数据
		Integer loginNum = RandomUtils.nextInt(100, 200);//TODO
		Integer videoViewNum = RandomUtils.nextInt(100, 200);//TODO
		Integer courseNum = RandomUtils.nextInt(100, 200);//TODO
		
		statisticsDaily.setVideoViewNum(videoViewNum);
		statisticsDaily.setCourseNum(courseNum);
		statisticsDaily.setLoginNum(loginNum);
		
		baseMapper.insert(statisticsDaily);
		
		return fruit.ok().data("registerCount", statisticsDaily);
		
		

		
		
		
	/*	//删除已存在的统计对象

		QueryWrapper<StatisticsDaily> dayQueryWrapper = new QueryWrapper<>();
		dayQueryWrapper.eq("date_calculated", day);
		baseMapper.delete(dayQueryWrapper);



		//获取统计信息
		Integer registerNum = (Integer) ucenterClient.registerCount(day).getData().get("countRegister");
		Integer loginNum = RandomUtils.nextInt(100, 200);//TODO
		Integer videoViewNum = RandomUtils.nextInt(100, 200);//TODO
		Integer courseNum = RandomUtils.nextInt(100, 200);//TODO

		//创建统计对象

		StatisticsDaily daily = new StatisticsDaily();
		daily.setRegisterNum(registerNum);
		daily.setLoginNum(loginNum);
		daily.setVideoViewNum(videoViewNum);
		daily.setCourseNum(courseNum);
		daily.setDateCalculated(day);
		baseMapper.insert(daily);*/
	}
	
	
	// 图标显示，返回日期数据，和数量json数组
	@Override
	public Map<String, Object> getChartData(String begin, String end, String type) {
		// 根据条件查询对应的数据
		
		System.out.println("开始"+begin);
		System.out.println("ENd"+end);
		System.out.println("type"+type);
		QueryWrapper<StatisticsDaily> dayQueryWrapper = new QueryWrapper<>();

		dayQueryWrapper.between("date_calculated", begin, end);

		dayQueryWrapper.select("date_calculated",type);

		List<StatisticsDaily> dayList = baseMapper.selectList(dayQueryWrapper);

		// 应为返回有两部分数据，日期和数量，所以要创建两个list
		// 前端要求数组json结构，对应后端java代码是list集合
		// 创建两个lisy集合，一个日期一个数量

		List<String> date_calculated = new ArrayList<String>();// 日期
		List<Integer> numdateList = new ArrayList<Integer>();//数量

		// 遍历查询出来的数据，把日期和数量添加到对应的list集合中
		for (int i = 0; i < dayList.size(); i++) {
			StatisticsDaily daily = dayList.get(i);
			//封装日期集合
			date_calculated.add(daily.getDateCalculated());

			switch (type) {
				case "register_num"://学员[注册数]统计
					numdateList.add(daily.getRegisterNum());
					System.out.println("学员[注册数]统计"+daily.getRegisterNum());
					break;
				case "login_num"://学员[登录数]统计
					numdateList.add(daily.getLoginNum());
					break;
				case "video_view_num"://[课程播放数]统计
					numdateList.add(daily.getVideoViewNum());
					break;
				case "course_num"://[每日课程数]统计
					numdateList.add(daily.getCourseNum());
					break;
				default:
					break;
			}
		}
		Map<String, Object> map = new HashMap<>();
		map.put("date_calculated", date_calculated);// 日期
		map.put("numdateList", numdateList);// 数量
		System.out.println("让对方播不播v ："+map);
		return map;
	}
	
	
//	@Override
//	public Map<String, Object> getChartData(String type, String begin, String end) {
//		QueryWrapper<StatisticsDaily> wrapper = new QueryWrapper<>();
//		wrapper.between("date_calculated",begin,end);
//		wrapper.select("date_calculated",type);
//		List<StatisticsDaily> staList = baseMapper.selectList(wrapper);
//		//要求json数组，对应后端list集合，需要日期list和数量list
//		List<String> dateList = new ArrayList<>();
//		List<Integer> numList = new ArrayList<>();
//		for (StatisticsDaily statisticsDaily : staList) {
//			dateList.add(statisticsDaily.getDateCalculated());
//			switch (type){
//				case "login_num":
//					numList.add(statisticsDaily.getLoginNum());
//					break;
//				case "register_num":
//					numList.add(statisticsDaily.getRegisterNum());
//					break;
//				case "video_view_num":
//					numList.add(statisticsDaily.getVideoViewNum());
//					break;
//				case "course_num":
//					numList.add(statisticsDaily.getCourseNum());
//					break;
//				default:
//					break;
//			}
//		}
//		Map<String,Object> map = new HashMap<>();
//		map.put("date_calculatedList",dateList);
//		map.put("numDataList",numList);
//		return map;
//	}
}
